package rstplus;

/*
 * RST - Russian SMS transliterator
 * Copyleft 2011
 * Author: Alexey Shat'ko (Алексей Шатько), rstapp@yandex.ru
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this source code (in file copying.txt); if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


import java.io.*;
import javax.microedition.rms.*;

public final class SettingsManager {

    private static final String RS_NAME = "rstplus.settings";
    private static RecordStore rs = null;
    private static int rsOpen = 0;
    private static boolean useX;
    private static boolean use4;
    private static boolean omitSoftSign;
    private static int historySize;
    private static boolean swapButtons;
    private static boolean enablePhonebook;
    public static final boolean DEFAULT_USE_X = false;
    public static final boolean DEFAULT_USE_4 = false;
    public static final boolean DEFAULT_OMIT_SOFT_SIGN = false;
    public static final int DEFAULT_HISTORY_SIZE = 10;
    public static final boolean DEFAULT_ENABLE_PHONEBOOK = false;

    private SettingsManager() {
    }

    public static int getHistorySize() {
        return historySize;
    }

    public static void setHistorySize(int historySize) {
        if (historySize < 0) {
            throw new IllegalArgumentException();
        }
        SettingsManager.historySize = historySize;
    }

    public static boolean isUse4() {
        return use4;
    }

    public static void setUse4(boolean use4) {
        SettingsManager.use4 = use4;
    }

    public static boolean isUseX() {
        return useX;
    }

    public static void setUseX(boolean useX) {
        SettingsManager.useX = useX;
    }

    public static boolean isOmitSoftSign() {
        return omitSoftSign;
    }

    public static void setOmitSoftSign(boolean omitSoftSign) {
        SettingsManager.omitSoftSign = omitSoftSign;
    }

    public static boolean isSwapButtons() {
        return swapButtons;
    }

    public static void setSwapButtons(boolean swapButtons) {
        SettingsManager.swapButtons = swapButtons;
    }

    public static boolean isPhonebookEnabled() {
        return enablePhonebook;
    }

    public static void setPhonebookEnabled(boolean enablePhonebook) {
        SettingsManager.enablePhonebook = enablePhonebook;
    }

    public static void load() throws RecordStoreException {
        byte[] record = null;
        rsOpen();
        try {
            RecordEnumeration re = rs.enumerateRecords(null, null, false);
            try {
                if (re.hasNextElement()) {
                    record = re.nextRecord();
                    if (re.hasNextElement()) {
                        throw new RecordStoreException();
                    }
                }
            } finally {
                re.destroy();
            }
        } finally {
            rsClose();
        }
        if (record == null) {
            useX = DEFAULT_USE_X;
            use4 = DEFAULT_USE_4;
            omitSoftSign = DEFAULT_OMIT_SOFT_SIGN;
            historySize = DEFAULT_HISTORY_SIZE;
            enablePhonebook = DEFAULT_ENABLE_PHONEBOOK;
            save();
        } else {
            try {
                DataInputStream dis = new DataInputStream(new ByteArrayInputStream(record));
                try {
                    useX = dis.readBoolean();
                    use4 = dis.readBoolean();
                    omitSoftSign = dis.readBoolean();
                    historySize = dis.readInt();
                    swapButtons = dis.readBoolean();
                    enablePhonebook = dis.readBoolean();
                } finally {
                    dis.close();
                }
            } catch (IOException exc) {
                throw new RuntimeException();
            }
        }
    }

    public static void save() throws RecordStoreException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        try {
            try {
                dos.writeBoolean(useX);
                dos.writeBoolean(use4);
                dos.writeBoolean(omitSoftSign);
                dos.writeInt(historySize);
                dos.writeBoolean(swapButtons);
                dos.writeBoolean(enablePhonebook);
            } finally {
                dos.close();
            }
        } catch (IOException exc) {
            throw new RuntimeException();
        }
        byte[] record = baos.toByteArray();
        rsOpen();
        try {
            int id = -1;
            RecordEnumeration re = rs.enumerateRecords(null, null, false);
            try {
                if (re.hasNextElement()) {
                    id = re.nextRecordId();
                    if (re.hasNextElement()) {
                        throw new RecordStoreException();
                    }
                }
            } finally {
                re.destroy();
            }
            if (id < 0) {
                rs.addRecord(record, 0, record.length);
            } else {
                rs.setRecord(id, record, 0, record.length);
            }
        } finally {
            rsClose();
        }
    }

    public static void clear() throws RecordStoreException {
        if ((rs != null) && (rsOpen > 0)) {
            rs.closeRecordStore();
        }
        RecordStore.deleteRecordStore(RS_NAME);
        if (rsOpen > 0) {
            rs = RecordStore.openRecordStore(RS_NAME, true);
        }
        load();
    }

    private static void rsOpen() throws RecordStoreException {
        if (rsOpen <= 0) {
            rs = RecordStore.openRecordStore(RS_NAME, true);
            rsOpen = 1;
        } else {
            rsOpen++;
        }
    }

    private static void rsClose() throws RecordStoreException {
        if (rsOpen > 1) {
            rsOpen--;
        } else if (rsOpen == 1) {
            rs.closeRecordStore();
            rs = null;
            rsOpen = 0;
        }
    }
}
